﻿using System;
using System.Configuration;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using OSCSessionCountDownTimer.Properties;

namespace OSCSessionCountDownTimer
{
    public partial class MainForm : Form
    {
        private const int WM_NCHITTEST = 0x84;
        private readonly IntPtr HTCLIENT = (IntPtr)1;
        private readonly IntPtr HTCAPTION = (IntPtr)2;

        private enum State
        {
            StartPosition,
            Running,
            Aborted,
            Finished
        }

        private State _State;

        private int _TimeLimit = 45;
        
        private int _SecondsToGo;

        private readonly Bitmap[] _NumImages = new[] {
            Resources.num0,Resources.num1,Resources.num2,Resources.num3,Resources.num4,
            Resources.num5,Resources.num6,Resources.num7,Resources.num8,Resources.num9,
        };

        private readonly Bitmap[] _ProgressImages = new[] {
            Resources.prog00,Resources.prog01,Resources.prog02,Resources.prog03,
            Resources.prog04,Resources.prog05,Resources.prog06,Resources.prog07,
            Resources.prog08,Resources.prog09,Resources.prog10,Resources.prog11,
            Resources.prog12,
        };

        public MainForm()
        {
            InitializeComponent();
            var timeLimit = ConfigurationManager.AppSettings["timeLimit"];
            if (string.IsNullOrEmpty(timeLimit) == false)
            {
                _TimeLimit = int.Parse(timeLimit);
            }

            ResetCounter();
            //DEBUG: timer1.Interval = 10;
        }
    
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_NCHITTEST)
            {
                base.WndProc(ref m);
                if (m.Result != HTCLIENT) m.Result = HTCAPTION;
            }
            else
                base.WndProc(ref m);
        }

        private void MainForm_Click(object sender, EventArgs e)
        {
            switch (_State)
            {
                case State.StartPosition:
                    timer1.Start();
                    _State = State.Running;
                    break;
                case State.Running:
                    timer1.Stop();
                    _State = State.Aborted;
                    break;
                case State.Finished:
                case State.Aborted:
                    ResetCounter();
                    _State = State.StartPosition;
                    break;
            }
        }

        public void ResetCounter()
        {
            _State = State.StartPosition;
            _SecondsToGo = _TimeLimit * 60;
            UpdateDisplay();
        }

        private void UpdateDisplay()
        {
            this.Opacity = 0.5;

            var m = _SecondsToGo / 60;
            var m1 = m / 10;
            var m2 = m % 10;
            picM1.Image = _NumImages[m1];
            picM2.Image = _NumImages[m2];

            var s = _SecondsToGo % 60;
            var s1 = s / 10;
            var s2 = s % 10;
            picS1.Image = _NumImages[s1];
            picS2.Image = _NumImages[s2];

            var p = 12 * (_TimeLimit * 60 - _SecondsToGo) / (_TimeLimit * 60);
            picProgress.Image = _ProgressImages[p];

            this.BringToFront();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            _SecondsToGo--;
            if (_SecondsToGo < 0)
            {
                timer1.Stop();
                _State = State.Finished;
            }
            else
            {
                UpdateDisplay();
            }
        }
    }
}
